package com.mvvm.start.common.utils

import android.app.Activity
import android.content.Context
import android.util.DisplayMetrics
import android.view.WindowManager
import com.mvvm.start.base.BaseApplication
import com.mvvm.start.base.BaseApplication.Companion.context
import com.mvvm.start.base.utils.loge

/**
 * @author
 * @功能:获取屏幕的宽高
 * @Creat 12/23/20 3:45 PM
 * @Compony 465008238@qq.com
 */
object DisplayUtil {
    /**
     *获取屏幕的高度 用于修正移动悬浮窗时越界的问题
     */
    fun getSceenHeight(windowManager: WindowManager): Int {
        var dm = DisplayMetrics()
        windowManager.defaultDisplay.getRealMetrics(dm)
        return dm.heightPixels
    }


    fun getSceenHeight(activity: Context): Int {
        val dm = DisplayMetrics()
        (activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay.getRealMetrics(
            dm
        )
        return dm.heightPixels
    }

    fun getSceenHeight(activity: Activity): Int {
        var dm = DisplayMetrics()
        activity.windowManager.defaultDisplay.getRealMetrics(dm)
        dm.heightPixels.loge("heightPixels")
        return dm.heightPixels
    }

    /**
     *  有时候获取的不准确
     *  获取屏幕高度
     */
    fun getScreenHeight(): Int {
        var heightPixels = 1920
        val windowManager = BaseApplication.context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        if (windowManager != null) {
            val outMetrics = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(outMetrics)
            heightPixels = outMetrics.heightPixels
        }
        var dm = DisplayMetrics()
        return heightPixels
    }


    /**
     * 获取屏幕宽度
     */
    fun getScreenWidth(): Int {
        var widthPixels = 1080
        val windowManager = BaseApplication.context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        if (windowManager != null) {
            val outMetrics = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(outMetrics)
            widthPixels = outMetrics.widthPixels
//            val heightPixels = outMetrics.heightPixels
        }
        return widthPixels
    }


    /**
     *获取状态栏的高度
     */
    fun getStatusBarHeight(): Int {
        val resourceId: Int =
            context.resources.getIdentifier("status_bar_height", "dimen", "android")
        return context.resources.getDimensionPixelSize(resourceId)
    }

}